package com.sgb.kjwl.view.ui.adapter.self.packers;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.SelfTeamWorkerEntity;
import com.sgb.kjwl.view.ui.adapter.packers.FindPeoplePicAdapter;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;


import java.util.List;

public class SelfTeamWorkerAdapter extends BaseRecycleAdapter<SelfTeamWorkerEntity> {

    private Button delView;
    private Button editView;
    private RecyclerView recyclerView;
    private RelativeLayout relativeLayout;
    private ItemCallBack itemCallBack;
    private FindPeoplePicAdapter adapter;
    private RequestManager requestManager;

    public SelfTeamWorkerAdapter(Context context, List<SelfTeamWorkerEntity> data, RequestManager requestManager) {
        super(context, R.layout.item_self_team_worker, data);
        this.requestManager = requestManager;
    }

    @Override
    public void convert(ItemViewHolder holder, final SelfTeamWorkerEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        delView = holder.getItem().findViewById(R.id.btn_delete);
        relativeLayout = holder.itemView.findViewById(R.id.root_layout);
        delView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCallBack != null) {
                    itemCallBack.del(entity.getNo());
                }
            }
        });
        editView = holder.getItem().findViewById(R.id.btn_edit);
        // editView.setVisibility(View.GONE);
        editView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCallBack != null) {
                    itemCallBack.edit(entity.getNo());
                }
            }
        });
        ImageView workerAvatar = holder.itemView.findViewById(R.id.iv_worker_avatar);;
        RequestOptions options = new RequestOptions();
        options.placeholder(R.mipmap.worker_avatar);
        options.transform(new CircleCrop());
        if (TextUtils.isEmpty(entity.getAvatarAddress())){
            requestManager.load(R.mipmap.worker_avatar).into(workerAvatar);
        }else {
            requestManager.load(entity.getAvatarAddress()).apply(options).into(workerAvatar);
        }
        TextView TvWorkerInfo = holder.itemView.findViewById(R.id.workerInfo);
        TvWorkerInfo.setText(String.format("%s岁  |  %s年工龄", entity.getAge(), entity.getWorkYear()));
        if (null == entity.getIdentity() || TextUtils.isEmpty(entity.getIdentity())) {
            holder.itemView.findViewById(R.id.tv_identity).setVisibility(View.GONE);
        } else {
            holder.itemView.findViewById(R.id.tv_identity).setVisibility(View.VISIBLE);
        }
        if (null == entity.getWorkTypeName() || TextUtils.isEmpty(entity.getWorkTypeName())) {
            holder.itemView.findViewById(R.id.tv_worker_type).setVisibility(View.GONE);
        } else {
            holder.itemView.findViewById(R.id.tv_worker_type).setVisibility(View.VISIBLE);
        }
        if (null == entity.getProficiencyName() || TextUtils.isEmpty(entity.getProficiencyName())) {
            holder.itemView.findViewById(R.id.tv_proficiencyName).setVisibility(View.GONE);
        } else {
            holder.itemView.findViewById(R.id.tv_proficiencyName).setVisibility(View.VISIBLE);
        }
    }

    public void setItemCallBack(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public interface ItemCallBack {
        void del(String id);

        void edit(String id);
    }
}
